$(function() {
    if (typeof(Storage) !== "undefined")
    {
        $('.wd-loading').show();

        $.post(localStorage.domain_app+"/webservice/budget/index.php", {
            page: 1,
            user_id: localStorage.userID
        }, function(res) {
            $('#unused_money').html(res.unused_money);
            $('#budget_goal').val(res.goal);
            $('#budget_spent').val(res.spent);
            $('#budget_items').html(res.item_list);
            if(res.notice_msg!=""){
               $('#notice_msg').children().html(res.notice_msg);
               $('#notice_msg').show();
            }
            $('#page').val(res.page);
            $('#end_paging').val(res.end_paging);
            $('.wd-loading').hide();
            save_budget_item_id();
            delete_budget_item();
        }, "json");
        var btn_edit_budget = false;
        $(".btn-edit-total-budget").click(function() {
            var btn_edit = $(this);
            if (btn_edit_budget == true) {
                return false;
            }
            btn_edit_budget = true;
            if (btn_edit.hasClass("edit-budget")) {
                $.post(localStorage.domain_app+"/webservice/budget/updatelBudgetGoal.php", {
                    total_budget: $("#budget_goal").val(),
                    user_id: localStorage.userID
                }, function(res) {
                    if (res.error == 0) {
                        $('#unused_money').html(res.unused_money);
                        $('#budget_goal').val(res.goal);
                        $("#budget_goal").prop('readonly', true);
                        btn_edit.removeClass("edit-budget");
                        $(".ico_edit_goal").show();
                        $(".ico_save_goal").hide();
                        print_alert(res.msg);
                    } else {
                        print_alert(res.msg);

                    }
                }, "json");

            } else {
                btn_edit.addClass("edit-budget");
                $("#budget_goal").prop('readonly', false);
                $("#budget_goal").focus();
                $(".ico_edit_goal").hide();
                $(".ico_save_goal").show();
            }
            btn_edit_budget = false;
        });
    }
});
function save_budget_item_id() {
    if (typeof(Storage) !== "undefined")
    {
        $(".wd-budget-edit-item").click(function() {
            sessionStorage.budget_item_id = $(this).attr("rel");
            wd_redirect("editBudget.html");
            return true;
        });

    }
    else
    {
        return false;
    }

}
function actionDeleteItem(button, item_id) {
    if (button == 1) {
        var user_id = 0;
        if (typeof(Storage) !== "undefined")
        {
            user_id = localStorage.userID;
        }
        $.post(localStorage.domain_app+"/webservice/budget/deleteBudgetItem.php", {
            item_id: item_id,
            user_id: localStorage.userID
        }, function(res) {
            btn_delete_item = false;
            console.log(res);
            if (res.error == 0) {
                print_alert(res.msg);
                document.location.reload(true);
            } else {
                print_alert(res.msg);

            }
        }, "json");
    } else {
        btn_delete_item = false;
        return false;
    }
}
var btn_delete_item = false;
function delete_budget_item() {

    $(".wd-budget-delete-ico").click(function() {
        if (btn_delete_item == false) {
            btn_delete_item = true;
            var item_id = $(this).attr("rel");
            navigator.notification.confirm(
                    "Are you sure you want to delete this item?",
                    function(buttonIndex) {
                        actionDeleteItem(buttonIndex, item_id);
                    }
            , // callback to invoke with index of button pressed
                    'Wedding Helper', // title
                    'OK,Cancel'          // buttonLabels
                    );
        }

        

    });
}